var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var db = require("./models/db.js");

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var swiper = require('./routes/swiper');
var typelist = require('./routes/typelist');
var recommend = require('./routes/recommend');
var floor = require('./routes/floor');
var modular = require('./routes/modular');
var goodslist = require('./routes/goodslist');
var searchgoods = require('./routes/searchgoods');
var onegoods = require('./routes/onegoods');
var preorder = require('./routes/preorder');
var pay = require('./routes/pay');
var paycallbck = require('./routes/paycallbck');
var orderlist = require('./routes/orderlist');
var cancelOrder = require('./routes/cancelOrder');
var checkOrder = require('./routes/checkOrder');
var uploadPic = require('./routes/uploadPic');



var app = express();
var http = require('http');
var server = http.createServer(app);
app.set('trust proxy', true);// 设置以后，req.ips是ip数组；如果未经过代理，则为[]. 若不设置，则req.ips恒为[]

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use('/api/swiper', swiper);
app.use('/api/Class', typelist);
app.use('/api/recommend', recommend);
app.use('/api/floor', floor);
app.use('/api/promotion/more', modular);
app.use('/api/goods', searchgoods);
app.use('/api/onegoods', onegoods);
app.use('/api/goods/class', goodslist);
//订单
app.use('/api/pay/update', preorder);
app.use('/api/pay/action',paycallbck);
app.use('/api/notpay',orderlist);
app.use('/api/pay/rightnow',pay);
app.use('/api/order/del', cancelOrder);
app.use('/api/order/more',checkOrder);

app.use('/api/uploadPic', uploadPic);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
